package com.d09cnpm2.group8.map;

import java.util.ArrayList;
import java.util.Map;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;

import com.google.android.gms.maps.model.LatLng;

public class GetDirectionAsyncTask extends
	AsyncTask<Map<String, String>, Object, ArrayList<LatLng>> {
    public static final String CUR_LAT = "currentLat";
    public static final String CUR_LNG = "currentLng";
    public static final String DES_LAT = "desLat";
    public static final String DES_LNG = "desLng";
    public static final String DIRECTION_MODE = "directionMode";
    private Dialog progressDialog;
    private Exception exception;
    private String TAG = "---------asynctask ---------->";
    private MapV2Activity activity;

    public GetDirectionAsyncTask(MapV2Activity activity) {
	super();
	this.activity = activity;
    }

    @Override
    protected void onPostExecute(ArrayList<LatLng> result) {
	progressDialog.dismiss();
	if (exception == null) {
	  //  Log.e(TAG, result.size() + "");
	    activity.drawRoutes(result);
	} else {
	    progressException();
	}
    }

    private void progressException( ) {
	Toast.makeText(activity, "error when retrieving data", 3000).show();
    }

    @Override
    protected void onPreExecute() {
	progressDialog = new ProgressDialog(activity);
	progressDialog.show();
    }
    

    @Override
    protected ArrayList<LatLng> doInBackground(Map<String, String>... params) {
	Map<String, String> paramMap = params[0];
	try {
	    LatLng fromPosition = new LatLng(Double.valueOf(paramMap
		    .get(CUR_LAT)), Double.valueOf(paramMap.get(CUR_LNG)));
	    LatLng toPosition = new LatLng(
		    Double.valueOf(paramMap.get(DES_LAT)),
		    Double.valueOf(paramMap.get(DES_LNG)));
	    GMapV2Direction md = new GMapV2Direction();
	    String result =md.getJsonResult(fromPosition, toPosition);
	    Log.e(TAG, result);
	    ArrayList<LatLng> directionPoints = md.getRoutes(result);
	    return directionPoints;
	} catch (Exception e) {
	 
	    exception = e;
	    return null;
	}
    }

}
